<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Controller for managing subscriptions.
 * @package backend-subscriptions/conrollers
 * @category Controller
 * 
 * @author Mark Angelo Angulo
 * @copyright (c) 2015
 */
class Subscription extends Backend_Controller {

    /**
     *  Constructor method.
     */
    function __construct()
    {
        parent::__construct();
        
        $this->load->model('subscription_model');
        
        $this->load->helper('cms');
        $this->load->helper('subscription');
        
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<div class="clear-fix"></div><small>', '</small>');
        
        init_subscription();
    }

    /**
     * Default method called when nothing is specified.
     * Displays dashboard.
     * @access public
     */
    public function index()
    {
        view_subscriptions_list();
    }
    
    public function subscriptions_datatable()
    {
        if( $this->input->is_ajax_request() )
        {
            echo $this->subscription_model
                    ->all_datatable
                    (
                        array(), 
                        $this->alias, 
                        $this->custom_columns, 
                        'subscriptions', 
                        TRUE
                    );
        } else
        {
            view_subscriptions_list();
        }
    }
    
    /*
     * Method called for adding a new record
     * @access public
     */
    public function add()
    {
        if( isset($_POST) && !empty($_POST) )
        {
            if ( valid_add_subscription() )
            {
                $subscription = array();
                set_value('name')           ? $subscription['name']           = clean_text( set_value('name') )               : '';
                set_value('description')    ? $subscription['description']    = clean_text( set_value('description') )        : '';
                set_value('status')         ? $subscription['status']         = clean_text( set_value('status') )             : 'active';
                
                $this->subscription_model->insert($subscription);

                // add another
                if( $this->input->post('add-another') )
                {
                    $this->form_validation->unset_field_data();
                    view_subscription_form('Add Subscription', 'add_success');
                } else
                {
                    view_subscriptions_list(NULL, 'add_success');
                }
                
            } else
            {
                view_subscription_form('Add Subscription', 'add_failed');
            }
            
        } else
        {
            view_subscription_form('Add Subscription');
        }
    }
    
    /**
     * Method called for editing a new record.
     * @access public
     */
    public function edit()
    {
        $params = $this->uri->uri_to_assoc();

        if( isset($_POST) && ! empty($_POST) )
        {
            if ( valid_edit_subscription() )
            {
                $subscription = array();
                set_value('name')           ? $subscription['name']           = clean_text( set_value('name') )               : '';
                set_value('description')    ? $subscription['description']    = clean_text( set_value('description') )        : '';
                set_value('status')         ? $subscription['status']         = clean_text( set_value('status') )             : '';
                $subscription['date_modified']    = date('Y-n-d H:i:s');
                
                $this->subscription_model->update( array('id' => set_value('id')), $subscription);
                view_subscriptions_list(NULL, 'edit_success');
            } else
            {
                $subscription = $this->subscription_model->find(set_value('id'))->get();
                view_subscription_form('Edit Subscription', 'edit_failed', $subscription);
            }
        } else
        {
            if( isset($params['id']) && valid_get_id($params['id']) )
            {
                $subscription = $this->subscription_model->find($params['id'])->get();
                view_subscription_form('Edit Subscription', NULL, $subscription);
            } else
            {
                $this->show_404();
            }
        }
    }
    
    /**
     * Method called when deleting a record.
     * @access public
     */
    public function delete()
    {
        $params = $this->uri->uri_to_assoc();
        if( isset($params['id']) && valid_get_id($params['id']) )
        {
            // delete from database
            $deleted = $this->subscription_model->delete(array('id' => $params['id']));
            if($deleted === 'deleted')
            {
                view_subscriptions_list(NULL, 'delete_success');
            } else
            {
                view_subscriptions_list(NULL, 'archived');
            }
        } else
        {
            $this->show_404();
        }
    }
    
    /*
     * Preview information about a record
     * @access public
     */
    public function preview()
    {
        $params = $this->uri->uri_to_assoc();
        if( isset($params['id']) && valid_get_id($params['id']) )
        {
            view_subscription_preview($params['id']);
        } else
        {
            $this->show_404();
        }
    }
    
    /**
     * Method called when deleting multiple subscriptions.
     * @access public
     */
    public function delete_multiple()
    {        
        if(valid_ids())
        {
            $items = $this->input->post('items');
            $archived = $this->subscription_model->until_it_sleeps($items);
            
            if(count($archived) > 0)
            {
                view_list(NULL, 'archived');
            } else
            {
                view_list(NULL, 'delete_success');
            }
        } else
        {
            $this->show_404();
        }
    }
    
}

/* End of file subscription.php */
/* Location: ./application/subscriptions/backend-subscriptions/controllers/subscription.php */